#include "serversetting.h"
#include "ui_serversetting.h"
#include "common/common.h"
#include <QIcon>
#include "settings/parametersettings.h"
//#if QT_VERSION < 0X050000
//extern QWSInputMethod* gInputMethod;
//#endif
//const QString ServerSetting::IP_SETTING = "ip";
//const QString ServerSetting::PORT_SETTING = "port";

ServerSetting::ServerSetting(QWidget *parent) :
    BaseEditor(QModelIndex(),parent),
    ui(new Ui::ServerSetting)
{
    ui->setupUi(this);
    //this->setWindowFlags(Qt::Drawer/*&~Qt::WindowCloseButtonHint*/);
    //this->setGeometry(220,30,360,200);
    setWindowFlags(Qt::FramelessWindowHint);
    setGeometry(WINDOW_GEOMETRY_SHIFT(0.4,0.4,0));
    mIp = ParameterSettings::getSettingValue(ParameterSettings::COMMUNI_SERVER,"192.168.1.100"
                                             ,ParameterSettings::COMMUNI_GROUP).toString();
    mPort = ParameterSettings::getSettingValue(ParameterSettings::COMMUNI_PORT,"12306"
                                             ,ParameterSettings::COMMUNI_GROUP).toInt();

    QIntValidator* intValidator = new QIntValidator(1,99999);
    ui->le_ip->setInputMask("000.000.000.000");
    ui->le_port->setValidator(intValidator);
    ui->le_ip->setText(mIp);
    ui->le_port->setText(QString("%1").arg(mPort));
//#if QT_VERSION < 0X050000
//    QWSServer::setCurrentInputMethod(SyszuxIM::createSyszuxIM(this));
//#endif
//    connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(accept()));
//    connect(ui->buttonBox,SIGNAL(rejected()),this,SLOT(reject()));
}

ServerSetting::~ServerSetting()
{
//#if QT_VERSION < 0X050000
//    QWSServer::setCurrentInputMethod(gInputMethod);
//#endif
    delete ui;
}

void ServerSetting::ServerInfo(QString *ip, int *port)
{
    *ip = ParameterSettings::getSettingValue(ParameterSettings::COMMUNI_SERVER,"192.168.1.100"
                                             ,ParameterSettings::COMMUNI_GROUP).toString();
    *port = ParameterSettings::getSettingValue(ParameterSettings::COMMUNI_PORT,"12306"
                                               ,ParameterSettings::COMMUNI_GROUP).toInt();
}


void ServerSetting::on_btn_cancel_clicked()
{
    this->reject();
}

void ServerSetting::on_btn_ok_clicked()
{
    QString ip = ui->le_ip->text();
    int port = ui->le_port->text().toInt();
    if(mIp!=ip||mPort!=port)
    {
        mIp=ip;mPort=port;
        QStringList setNamelst;
        setNamelst<<ParameterSettings::COMMUNI_SERVER<<ParameterSettings::COMMUNI_PORT;
        QVariantList valuelst;
        valuelst<<ip<<port;
        ParameterSettings::saveSettingValue(setNamelst,valuelst,ParameterSettings::COMMUNI_GROUP);
        emit ServerInfoChanged("IP  :"+ip+"\nPort:"+QString::number(port));
    }
    this->accept();
}
